package com.isoftstone.project.system.controller;

import java.util.List;

import com.isoftstone.project.system.domain.SysRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.isoftstone.project.system.domain.SysUser;
import com.isoftstone.project.system.service.ISysUserService;
import com.isoftstone.framework.controller.BaseController;
import com.isoftstone.framework.domain.Page;
import com.isoftstone.framework.domain.Tidings;
import com.isoftstone.common.utils.ExcelUtil;
import com.isoftstone.framework.domain.PageData;
import org.springframework.web.servlet.ModelAndView;

/**
 * systemController
 *
 * @author guoX
 * @date 2020-10-27
 */
@Controller
@RequestMapping("/system/user")
public class SysUserController extends BaseController {
    private String prefix = "system/user";

    @Autowired
    private ISysUserService sysUserService;

    @GetMapping()
    public String user() {
        return prefix + "/user_list";
    }

    /**
     * 查询system列表
     */
    @PostMapping("/sysUserlistPage")
    @ResponseBody
    public Object getsysUserlistPage(Page page) {
        PageData pd = this.getPageData();
        page.setPd(pd);
        List<SysUser> list = sysUserService.getSysUserlistPage(page);
        return Tidings.success(page.getTotalResult(), list);
    }


    /**
     * 导出system列表
     */
    @PostMapping("/exportSysUser")
    @ResponseBody
    public Object exportSysUser(Page page) {
        PageData pd = this.getPageData();
        page.setPd(pd);
        page.setEntityOrField(false);
        List<SysUser> list = sysUserService.getSysUserlistPage(page);
        ExcelUtil<SysUser> util = new ExcelUtil<SysUser>(SysUser. class);
        return util.exportExcel(list, "user");
    }

    /**
     * 新增system
     */
    @GetMapping("/addSysUser")
    public String addSysUser() {
        return prefix + "/user_add";
    }


    /**
     * 新增保存system
     */
    @PostMapping("/addSysUser")
    @ResponseBody
    public Object insertSysUser(SysUser sysUser) {
        sysUserService.insertSysUser(sysUser);
        return Tidings.success();

    }

    /**
     * 修改system
     */
    @GetMapping("/toEditSysUser")
    public String toEditSysUser(String userId, ModelMap mmap) {
        SysUser sysUser =sysUserService.getSysUserById(userId);
        mmap.put("sysUser", sysUser);
        return prefix + "/user_edit";
    }

    /**
     * 修改保存system
     */
    @PostMapping("/updateSysUser")
    @ResponseBody
    public Object updateSysUser(SysUser sysUser) {
        sysUserService.updateSysUser(sysUser);
        return Tidings.success();
    }

    /**
     * 删除system
     */
    @PostMapping("/deleteSysUser")
    @ResponseBody
    public Object deleteSysUser(String userId) {
        sysUserService.deleteSysUserByIds(userId);
        return Tidings.success();
    }



}
